package com.yunxun.coinmessage.service;

import com.yunxun.coinmessage.dao.CoinDao;
import com.yunxun.coinmessage.dao.MesSourceDao;
import com.yunxun.coinmessage.entity.Coin;
import com.yunxun.coinmessage.templatedata.MsgResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * 币种的业务逻辑处理
 *
 */
@Service

public class CoinService {


    @Autowired
    CoinDao coinDao;
    @Autowired
    MesSourceDao mesSourceDao;
    MsgResult msgResult;
   @Transactional(rollbackFor =Exception.class)
    public MsgResult addCoin(Coin coin){
            int tag;
            List<Coin> coinList=coinDao.queryByName(coin.getCname());
            if(coinList.size()!=0){
                msgResult=new MsgResult(false,"该币种已存在，请勿重复添加");
                return msgResult;
            }
            tag=coinDao.addCoin(coin);

            if(tag>0){
                msgResult=new MsgResult(true,"添加成功");
                return msgResult;

            }else {
                msgResult=new MsgResult(false,"添加失败，请稍后重试");
                return msgResult;
            }
    }
    public Coin queryById(Integer cid){
        return  coinDao.queryById(cid);
    }


    public  MsgResult deleteById(Integer cid){
            int tag;
            tag=coinDao.deleteById(cid);

            if(tag>0){
                msgResult=new MsgResult(true,"删除成功");
                return msgResult;
            }else{
                msgResult=new MsgResult(false,"删除失败");
                return msgResult;
            }
    }

    public MsgResult updateById(Coin coin){
           int tag;
           tag=  coinDao.updateById(coin);
            if(tag>0){
                msgResult=new MsgResult(true,"更新成功");
                return msgResult;
            }else {
                msgResult=new MsgResult(false,"更新失败");
                return msgResult;
            }
    }

    public List<Coin> queryAll(){
        return  coinDao.queryAll();
    }
    public  List<Coin> queryByPage(int start,int limit){
        return  coinDao.queryByPage(start,limit);
    }
    public Integer queryTotal(){
        return coinDao.queryTotal();
    }

}
